package edu.ynmd.com.action;


import edu.ynmd.com.model.Stuinfo;
import edu.ynmd.com.service.ManageService;
import edu.ynmd.com.vo.BMIForm;
import edu.ynmd.com.vo.Result;
import edu.ynmd.com.vo.SfzForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class PublicAction {

    @Autowired
    private ManageService manageService;

    @PostMapping("getBmiValue")
    public Result getBimValue(@RequestBody BMIForm bimForm) throws Exception{
        Float resultf=bimForm.getTz()/(bimForm.getSg()*bimForm.getSg());
        String resultstr="";

        if (resultf<18.4){
            resultstr="偏瘦";
        }else if(resultf>=18.5&&resultf<=23.9){
            resultstr="正常";
        }else if(resultf>=24&&resultf<=27.9){
            resultstr="偏胖";
        }else {
            resultstr="肥胖";
        }
        Result r=new Result();
        r.setValue(resultf);
       r.setState(resultstr);

       switch (resultstr){
           case "偏瘦":
               r.setSugguest("多吃点");
           break;
           case"正常":
               r.setSugguest("别多吃，也别少吃");
           break;
           case "偏胖":
               r.setSugguest("少吃点");
            break;
           case"肥胖":
               r.setSugguest("别吃啦");
            break;
       }
        return r;
    }

    @GetMapping("testCrud")
    @ResponseBody
    public boolean testCrud() throws Exception{

        Stuinfo s=new Stuinfo();
        s.setXm("王航");
        s.setSfzh("100000000000");
        s.setCsd("云南玉溪九龙池");
        s.setCsrp("1997-09-19");
        s.setXb("男");
        return manageService.addStuinfo(s);
    }

    @PostMapping("getSFZInfo")
    @ResponseBody
    /**
     * 返回数据库中所有表的记录
     */
    public List<Stuinfo> getSFZInfo(@RequestBody SfzForm sfzForm) throws Exception{

        String xm=sfzForm.getXm();
        String sfzh=sfzForm.getSfzh();
        System.out.println("接收到的姓名是："+xm);
        System.out.println("接收到的身份证号是："+sfzh);

        String sfbm=sfzh.substring(0,2);
        System.out.println("省份编码是："+sfbm);
        String citybm=sfzh.substring(0,4);
        System.out.println("州市编码是："+citybm);
        String areabm=sfzh.substring(0,6);
        System.out.println("州县编码是："+areabm);

        String csrqstr=sfzh.substring(6,14);
        System.out.println("出生日期是："+csrqstr);

        String xbstr=sfzh.substring(16,17);
        System.out.println("性别是："+xbstr);

        String xbname=manageService.getSexNameByStr(xbstr);
        System.out.println("性别是:"+xbname);

        String sfstr=manageService.getProvinceNameByCode(sfbm);
        System.out.println("从数据库中取得的名称是："+sfstr);

        String citynamestr=manageService.getCityNameByCode(citybm);
        System.out.println("从数据库中取得的州市编码是："+citynamestr);

        String areanamestr=manageService.getAreaNameByCode(areabm);
        System.out.println("从数据库中获得的州区县是："+areanamestr);



        Stuinfo s=new Stuinfo();
        s.setXb(xbname);
        s.setCsd(sfstr+citynamestr+areanamestr);
        s.setSfzh(sfzh);
        s.setCsrp(csrqstr);
        s.setXm(xm);
        manageService.addStuinfo(s);

        return manageService.getStuinfoList();

    }

    @PostMapping("addStuinfo")
    @ResponseBody
    public  Boolean addStuinfo (@RequestBody SfzForm sfzForm)throws Exception{
        String xm=sfzForm.getXm();
        String sfzh=sfzForm.getSfzh();
        System.out.println("接收到的姓名是："+xm);
        System.out.println("接收到的身份证号是："+sfzh);

        String sfbm=sfzh.substring(0,2);
        System.out.println("省份编码是："+sfbm);
        String citybm=sfzh.substring(0,4);
        System.out.println("州市编码是："+citybm);
        String areabm=sfzh.substring(0,6);
        System.out.println("州县编码是："+areabm);

        String csrqstr=sfzh.substring(6,14);
        System.out.println("出生日期是："+csrqstr);

        String xbstr=sfzh.substring(16,17);
        System.out.println("性别是："+xbstr);

        String xbname=manageService.getSexNameByStr(xbstr);
        System.out.println("性别是:"+xbname);

        String sfstr=manageService.getProvinceNameByCode(sfbm);
        System.out.println("从数据库中取得的名称是："+sfstr);

        String citynamestr=manageService.getCityNameByCode(citybm);
        System.out.println("从数据库中取得的州市编码是："+citynamestr);

        String areanamestr=manageService.getAreaNameByCode(areabm);
        System.out.println("从数据库中获得的州区县是："+areanamestr);



        Stuinfo s=new Stuinfo();
        s.setXb(xbname);
        s.setCsd(sfstr+citynamestr+areanamestr);
        s.setSfzh(sfzh);
        s.setCsrp(csrqstr);
        s.setXm(xm);
        return manageService.addStuinfo(s);
    }

    @PostMapping("updateStuinfo")
    @ResponseBody
    public Boolean updateStuinfo(@RequestBody SfzForm sfzForm) throws Exception{
        String xm=sfzForm.getXm();
        String sfzh=sfzForm.getSfzh();
        System.out.println("接收到的姓名是："+xm);
        System.out.println("接收到的身份证号是："+sfzh);

        String sfbm=sfzh.substring(0,2);
        System.out.println("省份编码是："+sfbm);
        String citybm=sfzh.substring(0,4);
        System.out.println("州市编码是："+citybm);
        String areabm=sfzh.substring(0,6);
        System.out.println("州县编码是："+areabm);

        String csrqstr=sfzh.substring(6,14);
        System.out.println("出生日期是："+csrqstr);

        String xbstr=sfzh.substring(16,17);
        System.out.println("性别是："+xbstr);

        String xbname=manageService.getSexNameByStr(xbstr);
        System.out.println("性别是:"+xbname);

        String sfstr=manageService.getProvinceNameByCode(sfbm);
        System.out.println("从数据库中取得的名称是："+sfstr);

        String citynamestr=manageService.getCityNameByCode(citybm);
        System.out.println("从数据库中取得的州市编码是："+citynamestr);

        String areanamestr=manageService.getAreaNameByCode(areabm);
        System.out.println("从数据库中获得的州区县是："+areanamestr);



        Stuinfo s=new Stuinfo();
        s.setStuinfoid(sfzForm.getStuinfoid());//指定主键
        s.setXb(xbname);
        s.setCsd(sfstr+citynamestr+areanamestr);
        s.setSfzh(sfzh);
        s.setCsrp(csrqstr);
        s.setXm(xm);
        return manageService.updateStuinfo(s);
    }

    @PostMapping("deletStuinfo")
    @ResponseBody
    public boolean deletStuinfo(@RequestBody Stuinfo stuinfo)throws Exception{
        return manageService.deleteStuinfo(stuinfo.getStuinfoid());

    }

    @PostMapping("getSigleStuinfo")
    @ResponseBody
    public Stuinfo getSigleStuinfo(@RequestBody Stuinfo stuinfo)throws Exception{
        return manageService.getSingleStuinfo(stuinfo.getStuinfoid());
    }

    @GetMapping("getStuinfoList")
    @ResponseBody
    public List<Stuinfo> getStuinfoList()throws Exception{
        return manageService.getStuinfoList();
    }
}
